package com.aizhixin.lab.company.service;

import lombok.extern.slf4j.Slf4j;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.core.UserInforCore;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.company.domain.AccountDomain;
import com.aizhixin.lab.company.domain.ClassesDomain;
import com.aizhixin.lab.company.domain.OrganDomain;
import com.aizhixin.lab.company.domain.QueryUserDomain;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.entity.Classes;
import com.aizhixin.lab.company.entity.SystemSet;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.repository.ClassesRepository;
import com.aizhixin.lab.company.repository.SystemSetRepository;

@Slf4j
@Service
@Transactional
public class ClassesService {
	@Autowired
	private ClassesRepository classesRepository;
	@Autowired
	private PageJdbcUtil pageJdbcUtil;

	public Map<String, Object> save(OrganDomain domain) {
		Map<String, Object> result = new HashMap<>();
		Classes classes = null;
		if (null != domain.getId() && domain.getId() > 0) {
			classes = classesRepository.findOne(domain.getId());
			classes.setCollegeId(domain.getCollegeId());
			classes.setProfessionalId(domain.getProfessionalId());
			classes.setName(domain.getName());
			String sql="UPDATE t_user t SET t.PROFESSIONAL_ID="+domain.getProfessionalId()+",t.COLLEGE_ID="+domain.getProfessionalId()+" WHERE t.CLASS_ID="+domain.getId();
			pageJdbcUtil.update(sql);
		}else{
			classes = new Classes();

			BeanUtils.copyProperties(domain, classes);
		}
		
		classesRepository.save(classes);
		result.put(ApiReturnConstants.SUCCESS, true);

		return result;
	}
	
	public Map<String, Object> delete(OrganDomain domain) {
		Map<String, Object> result = new HashMap<>();
		Classes classes = classesRepository.findOne(domain.getId());
		if(null != classes) {
			classes.setDeleteFlag(DataValidity.INVALID.getState());
			classesRepository.save(classes);
			result.put(ApiReturnConstants.SUCCESS, true);
		}else {
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "班级信息不存在");
		}
		return result;
	}
	
	public Map<String, Object> deleteByIds(List<Long> ids) {
		Map<String, Object> result = new HashMap<>();
		List<Classes> classList = classesRepository.findAllByIdInAndDeleteFlag(ids,DataValidity.VALID.getState());
		if(null != classList && !classList.isEmpty()){
			for(Classes classes : classList){
				classes.setDeleteFlag(DataValidity.INVALID.getState());
			}
			classesRepository.save(classList);
			result.put(ApiReturnConstants.SUCCESS, true);
		}else{
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "班级信息不存在");
		}
		return result;
	}
	
	 RowMapper<ClassesDomain> rm = new RowMapper<ClassesDomain>() {

			@Override
			public ClassesDomain mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				// TODO Auto-generated method stub
				ClassesDomain domain = new ClassesDomain();
				domain.setId(rs.getLong("ID"));
				domain.setName(rs.getString("NAME"));
				domain.setCollegeId(rs.getLong("COLLEGE_ID"));
				domain.setCollegeName(rs.getString("COLLEGE_NAME"));
				domain.setProfessionalId(rs.getLong("PROFESSIONAL_ID"));
				domain.setProfessionalName(rs.getString("PROFESSIONAL_NAME"));
				domain.setCreatedDate(rs.getTimestamp("CREATED_DATE"));
				domain.setStuNum(rs.getInt("STU_NUM"));
				return domain;
			}
		};



	    public <T> Map<String, Object> findClassesPage(OrganDomain domain) {

			String querySql = "select c.`id`,c.`NAME`,c.COLLEGE_ID,co.`NAME` as COLLEGE_NAME,c.PROFESSIONAL_ID,p.`NAME` as PROFESSIONAL_NAME,c.CREATED_DATE,(SELECT count(*) FROM `t_user` u where u.CLASS_ID=c.ID and u.delete_flag =0) as STU_NUM FROM `t_classes` as c LEFT JOIN `t_professional` p ON c.PROFESSIONAL_ID = p.ID LEFT JOIN `t_college` co ON c.COLLEGE_ID = co.ID where c.DELETE_FLAG = 0 and p.DELETE_FLAG = 0 and co.DELETE_FLAG = 0 ";
			String countSql = "select count(*) FROM `t_classes` as c LEFT JOIN `t_professional` p ON c.PROFESSIONAL_ID = p.ID LEFT JOIN `t_college` co ON c.COLLEGE_ID = co.ID where c.DELETE_FLAG = 0 and p.DELETE_FLAG = 0 and co.DELETE_FLAG = 0 ";
			if (!StringUtils.isEmpty(domain.getName())) {
				querySql += " and c.NAME like '%" + domain.getName() + "%'";
				countSql += " and c.NAME like '%" + domain.getName() + "%'";
			}
			if (null != domain.getCollegeId() && domain.getCollegeId().longValue() > 0L) {
				querySql += " and c.COLLEGE_ID=" + domain.getCollegeId() + "";
				countSql += " and c.COLLEGE_ID=" + domain.getCollegeId() + "";
			}
			if (null != domain.getProfessionalId() && domain.getProfessionalId().longValue() > 0L) {
				querySql += " and c.PROFESSIONAL_ID=" + domain.getProfessionalId() + "";
				countSql += " and c.PROFESSIONAL_ID=" + domain.getProfessionalId() + "";
			}
			List<SortDTO> sort = new ArrayList<SortDTO>();
			SortDTO dto = new SortDTO();
			dto = new SortDTO();
			dto.setKey("c.ID");
			dto.setAsc(false);
			sort.add(dto);

			return pageJdbcUtil.getPageInfor(domain.getPageSize(),
					domain.getPageNumber(), rm, sort, querySql, countSql);
		}
		public Classes findClass(Long id){
	    	return classesRepository.findById(id);
		}
}
